import tkinter as tk
from tkinter import ttk, scrolledtext, messagebox
import os
import threading

class DouyinView:
    def __init__(self, controller):
        self.controller = controller
        self.root = tk.Tk()
        self.root.title("抖音视频下载器")
        self.root.geometry("600x400")
        
        # 设置样式
        style = ttk.Style()
        style.configure('TButton', padding=5)
        style.configure('TLabel', padding=5)
        
        self.create_widgets()
        
    def create_widgets(self):
        # 创建主框架
        main_frame = ttk.Frame(self.root, padding="10")
        main_frame.pack(fill=tk.BOTH, expand=True)
        
        # 输入区域
        input_frame = ttk.LabelFrame(main_frame, text="输入抖音分享链接", padding="5")
        input_frame.pack(fill=tk.X, pady=5)
        
        self.text_input = scrolledtext.ScrolledText(input_frame, height=3, width=50)
        self.text_input.pack(fill=tk.X, padx=5, pady=5)
        
        # 进度条区域
        progress_frame = ttk.Frame(main_frame)
        progress_frame.pack(fill=tk.X, pady=5)
        
        self.progress_var = tk.DoubleVar()
        self.progress_bar = ttk.Progressbar(progress_frame, variable=self.progress_var, maximum=100)
        self.progress_bar.pack(fill=tk.X, padx=5, pady=5)
        
        # 按钮区域
        button_frame = ttk.Frame(main_frame)
        button_frame.pack(fill=tk.X, pady=5)
        
        self.download_button = ttk.Button(button_frame, text="下载视频", command=self.on_download)
        self.download_button.pack(side=tk.LEFT, padx=5)
        
        self.clear_button = ttk.Button(button_frame, text="清空", command=self.on_clear)
        self.clear_button.pack(side=tk.LEFT, padx=5)
        
        self.open_folder_button = ttk.Button(button_frame, text="打开下载文件夹", command=self.on_open_folder)
        self.open_folder_button.pack(side=tk.LEFT, padx=5)
        
        # 状态区域
        status_frame = ttk.LabelFrame(main_frame, text="状态", padding="5")
        status_frame.pack(fill=tk.BOTH, expand=True, pady=5)
        
        self.status_text = scrolledtext.ScrolledText(status_frame, height=10, width=50)
        self.status_text.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
        self.status_text.config(state=tk.DISABLED)
        
    def on_download(self):
        text = self.text_input.get("1.0", tk.END).strip()
        # 在新线程中执行下载，避免界面卡顿
        threading.Thread(target=self.controller.handle_download, args=(text,), daemon=True).start()
        
    def on_clear(self):
        self.text_input.delete("1.0", tk.END)
        self.update_status("已清空输入框")
        
    def on_open_folder(self):
        self.controller.handle_open_folder()
        
    def update_status(self, message):
        def _update():
            self.status_text.config(state=tk.NORMAL)
            self.status_text.insert(tk.END, message + "\n")
            self.status_text.see(tk.END)
            self.status_text.config(state=tk.DISABLED)
        # 在主线程中更新UI
        self.root.after(0, _update)
        
    def show_error(self, message):
        def _show():
            messagebox.showerror("错误", message)
        self.root.after(0, _show)
        
    def show_success(self, message):
        def _show():
            messagebox.showinfo("成功", message)
        self.root.after(0, _show)
        
    def update_progress(self, progress):
        def _update():
            self.progress_var.set(progress)
        self.root.after(0, _update)
        
    def run(self):
        self.root.mainloop() 